<template>
  <div class="index">
    <div class="header flex">
      <div class="flex" v-for="(item, index) in tipData" :key="index">
        <div class="circle"></div>
        <div class="icon"><i :class="icons[index]"></i></div>
        <div class="right">
          <div class="title">{{ item.name }}</div>
          <div class="data">{{ item.value }}</div>
        </div>
      </div>
    </div>
    <div class="content">
      <div class="flex">
        <div class="left">
          <div class="title flex">
            <div class="text">文件类型数量统计</div>
            <el-radio-group v-model="fileType" size="small" @input="changeType">
              <el-radio-button label="1">本年数据</el-radio-button>
              <el-radio-button label="2">本月数据</el-radio-button>
              <el-radio-button label="3">本周数据</el-radio-button>
            </el-radio-group>
          </div>
          <div class="chart">
            <Doughnut refName="left" :optionData="fileTypeData" />
          </div>
        </div>
        <div class="right">
          <div class="title flex">
            <div class="text">各类型文件大小统计</div>
            <el-radio-group v-model="fileSize" size="small" @input="changeSize">
              <el-radio-button label="1">本年数据</el-radio-button>
              <el-radio-button label="2">本月数据</el-radio-button>
              <el-radio-button label="3">本周数据</el-radio-button>
            </el-radio-group>
          </div>
          <div class="chart">
            <Doughnut refName="right" :optionData="fileSizeData" />
          </div>
        </div>
      </div>
      <div class="line">
        <div class="title flex">
          <div class="text">各文件数据类型数量统计</div>
          <el-radio-group v-model="fileData" size="small" @input="changeData">
            <el-radio-button label="1">本年数据</el-radio-button>
            <el-radio-button label="2">本月数据</el-radio-button>
            <el-radio-button label="3">本周数据</el-radio-button>
          </el-radio-group>
        </div>
        <div id="chart"></div>
      </div>
    </div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import { leftChart, rightChart, getLineChart, getTip } from "@/api/data";

export default {
  name: "Index",
  data() {
    return {
      fileType: null,
      myChart: null,
      fileType: 1,
      fileSize: 1,
      fileData: 1,
      fileTypeData: null,
      fileSizeData: null,
      fileDataData: null,
      tipData: null,
      icons: [
        "el-icon-user",
        "el-icon-thumb",
        "el-icon-tickets",
        "el-icon-folder-opened",
        "el-icon-document-checked",
        "el-icon-video-play",
      ],
    };
  },
  mounted() {
    this.getTipData();
    this.getLeftChartData();
    this.getRightChartData();
    this.getLineChartData();
  },
  methods: {
    getTipData() {
      getTip().then((res) => {
        this.tipData = res.data;
      });
    },
    changeData(val) {
      this.fileData = val;
      this.getLineChartData();
    },
    getLineChartData() {
      getLineChart({ id: this.fileData }).then((res) => {
        this.fileDataData = res.data;
        this.initLineChart();
      });
    },
    changeSize(val) {
      this.fileSize = val;
      this.getRightChartData();
    },
    getRightChartData() {
      rightChart({ id: this.fileSize }).then((res) => {
        this.fileSizeData = res.data;
      });
    },
    changeType(val) {
      this.fileType = val;
      this.getLeftChartData();
    },
    getLeftChartData() {
      leftChart({ id: this.fileType }).then((res) => {
        this.fileTypeData = res.data;
      });
    },
    initLineChart() {
      if (this.myChart) {
        this.myChart.clear();
        this.myChart.dispose();
        this.myChart = null;
      }
      const chartDom = document.getElementById("chart");
      this.myChart = echarts.init(chartDom);

      let option = {
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: this.fileDataData && this.fileDataData.totalList,
          top: "2%",
          right: "4%",
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: this.fileDataData && this.fileDataData.timeList,
        },
        yAxis: {
          type: "value",
        },
        series: this.fileDataData && this.fileDataData.data,
      };

      option && this.myChart.setOption(option);
    },
  },
};
</script>
<style lang="scss" scoped>
.index {
  padding: 24px;
  width: 100%;
  background: #f0f2f5;
  .flex {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .header {
    & > div {
      position: relative;
      padding: 24px 0 24px 24px;
      background: #fff;
      border-radius: 12px;
      width: 240px;
      height: 140px;
      justify-content: flex-start;
      overflow: hidden;
      .circle {
        position: absolute;
        top: -80px;
        right: -80px;
        width: 160px;
        height: 160px;
        border: 24px solid #f3f8ff;
        background: #fff;
        border-radius: 50%;
      }
      .icon {
        margin-right: 20px;
        width: 56px;
        height: 56px;
        border-radius: 50%;
        border: 2px dashed #365eff;
        align-items: center;
        text-align: center;
        font-size: 38px;
        color: #365eff;
      }
      .right {
        position: relative;
        z-index: 10;
        height: 100%;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        .title {
          font-size: 26px;
        }
        .data {
          font-size: 22px;
          color: #365eff;
        }
      }
    }
  }
  .content {
    margin-top: 24px;
    padding: 36px 24px;
    width: 100%;
    border-radius: 12px;
    background: #fff;
    .left,
    .right {
      width: 46%;
    }
    .chart,
    #chart {
      height: 300px;
    }
    .title {
      .text {
        line-height: 16px;
        padding-left: 8px;
        border-left: 3px solid #365eff;
        font-weight: bold;
      }
    }
    .line {
      .title {
        width: 46%;
      }
    }
  }
}
</style>
